package com.peas.platform.module.icims.budget.apply.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peas.platform.common.core.dto.ResponseDto;
import com.peas.platform.framework.mybatisplus.utils.PageUtil;
import com.peas.platform.module.icims.budget.apply.api.req.AdjustBudgetOneAmount;
import com.peas.platform.module.icims.budget.apply.api.req.SaveBudgetOneCollectVo;
import com.peas.platform.module.icims.budget.apply.api.req.SaveBudgetOneVo;
import com.peas.platform.module.icims.budget.apply.api.req.SubmitBudgetOneVo;
import com.peas.platform.module.icims.budget.apply.controller.criteria.BudgetOneCollectCriteria;
import com.peas.platform.module.icims.budget.apply.dto.BudgetOneCollectDto;
import com.peas.platform.module.icims.budget.apply.dto.BudgetOneDto;
import com.peas.platform.module.icims.budget.apply.service.BudgetOneCollectService;
import com.peas.platform.module.icims.budget.apply.service.BudgetOneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "自定义接口-[预算控制]一上预算编制")
@RestController
@RequestMapping("/api/v1/customBudgetOne")
public class CustomBudgetOneApiController {

    @Resource
    private BudgetOneService budgetOneService;
    @Resource
    private BudgetOneCollectService budgetOneCollectService;

    @ApiOperation(value = "编辑暂存一上预算")
    @PostMapping("/stageBudgetOne")
    public ResponseDto<BudgetOneDto> stageBudgetOne(@RequestBody @Valid SaveBudgetOneVo vo) {
        BudgetOneDto dto = budgetOneService.saveBudgetOne(vo);
        return ResponseDto.getSuccessResponseDto(dto);
    }

    @ApiOperation(value = "编辑保存一上预算")
    @PostMapping("/saveBudgetOne")
    public ResponseDto<Boolean> saveBudgetOne(@RequestBody @Valid SaveBudgetOneVo vo) {
        budgetOneService.saveBudgetOne(vo);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "编辑提交一上预算")
    @PostMapping("/submitBudgetOne")
    public ResponseDto<Boolean> submitBudgetOne(@RequestBody @Valid SubmitBudgetOneVo vo) {
        budgetOneService.submitBudgetOne(vo);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "撤回一上预算")
    @PostMapping("/revocationBudgetOne/{id}")
    public ResponseDto<Boolean> revocationBudgetOne(@PathVariable("id") Long id) {
        budgetOneService.revocationBudgetOne(id);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "根据ID查询一上预算，包含统计金额")
    @GetMapping("/queryBudgetOneByIds/{ids}")
    public ResponseDto<List<BudgetOneDto>> queryBudgetOneByIds(@PathVariable(value = "ids") String ids) {
        List<Long> idList = new ArrayList<>();
        for (String id : StrUtil.split(ids, StrUtil.COMMA)) {
            idList.add(Long.valueOf(id));
        }
        List<BudgetOneDto> list = budgetOneService.queryBudgetOneById(idList);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "根据一级项目ID查询一上预算，包含统计金额")
    @GetMapping("/queryBudgetOneByProjectOneId/{id}")
    public ResponseDto<List<BudgetOneDto>> queryBudgetOneByProjectOneId(@PathVariable(value = "id") Long id) {
        List<BudgetOneDto> list = budgetOneService.queryBudgetOneByProjectOneId(id);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "根据组织架构ID查询一上预算，包含统计金额")
    @GetMapping("/queryBudgetOneByOrganizationId/{id}")
    public ResponseDto<List<BudgetOneDto>> queryBudgetOneByOrganizationId(@PathVariable(value = "id") Long id) {
        List<BudgetOneDto> list = budgetOneService.queryBudgetOneByOrganizationId(id);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "根据申报单位ID查询一上预算，包含统计金额")
    @GetMapping("/queryBudgetOneByApplyGovernmentId/{id}")
    public ResponseDto<List<BudgetOneDto>> queryBudgetOneByApplyGovernmentId(@PathVariable(value = "id") Long id) {
        List<BudgetOneDto> list = budgetOneService.queryBudgetOneByApplyGovernmentId(id);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "根据用款单位ID查询一上预算，包含统计金额")
    @GetMapping("/queryBudgetOneByUseGovernmentId/{id}")
    public ResponseDto<List<BudgetOneDto>> queryBudgetOneByUseGovernmentId(@PathVariable(value = "id") Long id) {
        List<BudgetOneDto> list = budgetOneService.queryBudgetOneByUseGovernmentId(id);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "编辑保存一上预算汇总")
    @PostMapping("/saveBudgetOneCollect")
    public ResponseDto<Boolean> saveBudgetOneCollect(@RequestBody @Valid SaveBudgetOneCollectVo vo) {
        budgetOneCollectService.saveBudgetOneCollect(vo);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "查询一上预算汇总，包含统计金额")
    @GetMapping("/queryBudgetOneCollectTotalAmount")
    public ResponseDto<Page<BudgetOneCollectDto>> queryBudgetOneCollectTotalAmount(BudgetOneCollectCriteria condition,
                                                                                   @ApiParam(name = "pageNum", value = "分页页码", example = "1") Integer pageNum,
                                                                                   @ApiParam(name = "pageSize", value = "分页每页大小", example = "10") Integer pageSize,
                                                                                   @ApiParam(name = "sortField", value = "排序字段", example = "id") String sortField,
                                                                                   @ApiParam(name = "sortType", value = "排序方式", example = "desc") String sortType) {
        Page<BudgetOneCollectDto> pager = PageUtil.genPager(pageNum, pageSize, sortField, sortType);
        BudgetOneCollectDto c = BeanUtil.copyProperties(condition, BudgetOneCollectDto.class);
        Page<BudgetOneCollectDto> dtos = budgetOneCollectService.page(pager, c);
        if (dtos != null && CollUtil.isNotEmpty(dtos.getRecords())) {
            for (BudgetOneCollectDto dto : dtos.getRecords()) {
                BigDecimal totalAmount = budgetOneCollectService.calcBudgetOneCollectTotalAmount(dto);
                dto.setTotalAmount(totalAmount);
            }
        }
        return ResponseDto.getSuccessResponseDto(dtos);
    }

    @ApiOperation(value = "调整一下控制金额")
    @PostMapping("/adjustBudgetOneAmount")
    public ResponseDto<Boolean> adjustBudgetOneAmount(@RequestBody @Valid AdjustBudgetOneAmount vo) {
        budgetOneService.adjustBudgetOneAmount(vo.getItems());
        return ResponseDto.getSuccessResponseDto(true);
    }

}
